import {
  getQueryParameter,
  getUpkeepList,
  getUpkeepPlanList,
} from 'service/actions/deviceUpkeep'
import { message } from 'antd'
export default {
  namespace: 'deviceUpkeep',
  state: {
    parameter: {},
    upkeep: {},
    plan: {},
    serachParams: {}
  },

  effects: {
    *getQueryParameter({ payload }, { put, call }) {
      const data = yield call(getQueryParameter, payload)
      if (data) {
        yield put({
          type: 'updateParameter',
          payload: { parameter: data.data }
        })
      }
    },
    *getUpkeepList({ payload }, { put, call }) {
      const data = yield call(getUpkeepList, payload || {})
      if (data && data.code === '000000') {
        yield put({
          type: 'updateUpkeep',
          payload: { upkeep: data.data }
        })
      }
    },
    *getUpkeepPlanList({ payload }, { put, call }) {
      const data = yield call(getUpkeepPlanList, payload)
      if (data) {
        if (data.code === '000000') {
          yield put({
            type: 'updatePlan',
            payload: {
              plan: {
                ...data.data,
                records: data.data.records.filter(item => {
                  return item.deleted !== 'Y'
                })
              }
            }
          })
        } else {
          message.error(data.mesg)
        }
      }
    }
  },

  reducers: {
    // 更新parameter
    updateParameter(state, { payload: { parameter } }) {
      return { ...state, parameter }
    },
    // 更新upkeep
    updateUpkeep(state, { payload: { upkeep } }) {
      return { ...state, upkeep }
    },
    // 更新plan
    updatePlan(state, { payload: { plan } }) {
      return { ...state, plan }
    },
    // 更细查询参数
    updateSerachParams(state, { payload: { serachParams } }) {
      return { ...state, serachParams }
    }
  }
}
